using System.Collections.Generic;
using LitJson;
using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    public partial class PanelMyTrainingDetail : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
        private List<string> list = new List<string>();

        public static void Open(JsonData json)
        {
            UIManagerComponent.instance.OpenUI<PanelMyTrainingDetail>(UILayer.Normal, RootComponent.instance, false)
                .ReqData(json);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelMyTrainingDetail>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelMyTrainingDetail", layer, isFloat);
            BindProperties(transform);

            BtnClose.SetClick(Close);

            ScrollView.SetCellUpdateCallback(async (RectTransform item, int index) =>
            {
                string url = list[index];
                Texture texture = await TextureHelper.LoadTextureRemote(url, true);
                if (!item) return;
                item.GetComponent<ImagePro>().sprite = TextureHelper.TextureToSprite(texture);
                item.gameObject.Get<GameObject>("Loading").SetActive(false);
                item.GetComponent<CButton>().SetClick(() => { PanelPhotoView.Open(texture); });
            });
        }

        private async void ReqData(JsonData json)
        {
            Debug.Log(JsonMapper.ToJson(json));
            string id = json["id"].ToString();
            int state = (int)json["state"];
            string time = TimeHelper.Parse2Time(json["created_at"].ToString(), true);
            string yellow = "#ffcc00";
            string red = "#ff0000";
            TextOrderNo.text = $"{I18N_order_no}：<color={yellow}>{id}</color>";
            if (state <= 1)
            {
                TextState.text = $"{I18N_state}：<color={yellow}>{I18N_review}</color>";
            }
            else if (state == 2)
            {
                TextState.text = $"{I18N_state}：<color={red}>{I18N_reject}</color>";
                TextState.text += $"\n{I18N_reason}：<color={red}>{json["reason"]}</color>";
            }
            else if (state == 3)
            {
                TextState.text = $"{I18N_state}：<color={yellow}>{I18N_passed}</color>";
            }

            TextTime.text = $"{I18N_time}：<color={yellow}>{time}</color>";

            Network.Res res = await Network.Get("/v1/reward/file/" + id + "?limit=1000");
            if (!res.success) return;
            JsonData listData = res.jsonData["data"]["list"];
            for (int i = 0; i < listData.Count; i++)
            {
                list.Add(listData[i]["file_path"].ToString());
            }

            ScrollView.SetTotalNumber(list.Count);
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }
    }
}